import 'package:flutter/material.dart';
import 'package:flutterhouse/api/house.dart';
import 'package:flutterhouse/pages/House/componeents/house_item.dart';
import 'package:flutterhouse/utils/Emitter.dart';

class HouseList extends StatefulWidget {
  const HouseList({super.key});

  @override
  State<HouseList> createState() => _HouseListState();
}

class _HouseListState extends State<HouseList> {
  List list = [];
  bool loading = false;
  @override
  void initState() {
    super.initState();
    getHouseList();
    // z注册事件
    regEvent();
  }

  regEvent() {
    // 监听事件
    eventBus.on<RefreshHouseListEvent>().listen((event) {
      getHouseList();
    });
  }

  getHouseList() async {
    // loading = true;
    final res = await getHouseListAPI();
    print('结果$res');
    list = res;
    // loading = false;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    if (loading) {
      return Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(
          color: Color.fromARGB(255, 254, 254, 254),
        ),
        alignment: Alignment.center,
        child: const CircularProgressIndicator(),
      );
    }
    return Scaffold(
      backgroundColor: const Color.fromARGB(255, 241, 238, 238),
      appBar: AppBar(
        title: const Text('我的房屋'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: Stack(
        children: [
          Padding(
            padding: const EdgeInsets.only(bottom: 80, left: 10, right: 10),
            child: ListView.separated(
              itemBuilder: (BuildContext context, int index) {
                return GestureDetector(
                  onTap: () {
                    Navigator.pushNamed(context, '/house_detail', arguments: {
                      'id': list[index]['id'],
                    });
                  },
                  child: HouseItem(
                    data: list[index],
                  ),
                );
              },
              separatorBuilder: (BuildContext context, int index) {
                return Container(
                  height: 10,
                  color: const Color.fromARGB(255, 241, 238, 238),
                );
              },
              itemCount: list.length,
            ),
          ),
          Positioned(
            bottom: 0,
            left: 0,
            right: 0,
            child: Container(
              padding: const EdgeInsets.all(10),
              color: Colors.transparent,
              child: Row(
                children: [
                  Expanded(
                    child: ElevatedButton(
                      onPressed: () {
                        Navigator.pushNamed(context, '/LocationList');
                      },
                      child: const Column(
                        children: [
                          SizedBox(height: 5),
                          Icon(Icons.add),
                          Text('添加房屋'),
                          SizedBox(height: 5),
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}
